package com.zjw.zy.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.zjw.zy.base.BaseViewModel
import com.zjw.zy.bean.HourlyWeather
import com.zjw.zy.bean.NearWeather
import com.zjw.zy.bean.Weather15Day
import com.zjw.zy.bean.Weather1Day
import com.zjw.zy.ext.requireService
import remotecall.RemoteCallManager

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/9/27 15:21
 * @description：天气相关
 */
class WeatherViewModel : BaseViewModel() {
    companion object {
        private const val TAG = "WeatherViewModel"
    }

    val mHourWeatherObserver =
        RemoteCallManager.defaultImpl().remoteCall<Weather1Day>() { weather ->
            weather?.let {
                m1HourWeatherLiveData.postValue(it)
            }
        }

    init {
        //注册逐小时天气观察
        requireService()?.observer1DayWeather(mHourWeatherObserver)
    }

    //十五天天气预报
    private val m15WeatherLiveData = MutableLiveData<Weather15Day?>()
    private val m1HourWeatherLiveData = MutableLiveData<Weather1Day?>()

    private val mNearWeatherNotify = MutableLiveData<NearWeather>()


    fun get15Weather(): LiveData<Weather15Day?> = m15WeatherLiveData
    fun getHourWeather(): LiveData<Weather1Day?> = m1HourWeatherLiveData

    //请求15天天气信息
    fun request15DayWeather() {
        requireService()?.request15DayWeather(
            RemoteCallManager.defaultImpl().remoteCall<Weather15Day>() {
                it?.let {
                    m15WeatherLiveData.postValue(it)
                }
            })
    }


    //逐小时天气信息
    fun request1HourWeather() {
        requireService()?.request1DayWeather(
            RemoteCallManager.defaultImpl().remoteCall<Weather1Day>() {
                it?.let {
                    m1HourWeatherLiveData.postValue(it)
                }
            })
    }

    override fun onCleared() {
        super.onCleared()
        requireService()?.remove1DayWeather(mHourWeatherObserver)
    }

    fun getCurrentHourWeather(): HourlyWeather? {
        return m1HourWeatherLiveData.value?.hourlyFcsts?.get(0)
    }
}